package core;

import gitobject.GitObject;
import gitobject.Index;
import repository.Repository;
import sha1.SHA1;

import java.io.File;
import java.util.Map;

public class JitStatus {
    public static void status(String currentPath) throws Exception {
        // currentPath是现有路径，从.git路径到现有路径的增添和改动需要追踪，子目录下的文件不需要追踪
        File indexFile = new File(Repository.getGitDir() + File.separator + "index");
        File currentDir = new File(currentPath);
        if (currentDir.isDirectory()) {
            File[] allFiles = currentDir.listFiles();
            int length = allFiles.length;

            Index index;
            if (indexFile.exists()) {
                index = Index.deserialize(indexFile);
                for (int i = 0; i < length; i++) {
                    boolean tracked = false;
                    String time = Index.getTime(allFiles[i]);

                    if (index.fileOb.containsKey(time)) {
                        tracked = true;
                        System.out.printf("Tracked:%s \n", allFiles[i].getCanonicalPath());
                        String hash = allFiles[i].isDirectory() ? SHA1.getHash(SHA1.getDirHashStr(allFiles[i]))
                                : SHA1.getHash(allFiles[i]);

                        File obj = new File(Repository.getGitDir() + File.separator + "objects" + File.separator
                                + hash.substring(0, 2) + File.separator + hash.substring(2));
                        if (!obj.exists()) {
                            System.out.printf("Modefied:%s \n", allFiles[i].getCanonicalPath());
                        }
                    }

                    if (!tracked) {
                        //System.out.printf("Untracked:%s \n", allFiles[i].getCanonicalPath());
                    }

                }

                index.writeObject();
            }

        } else {
            System.out.println("路径不存在!");
        }
    }

}
